package com.xiaolei48.service.impl;

import com.xiaolei48.entity.Foods;
import com.xiaolei48.mapper.FoodsMapper;
import com.xiaolei48.service.FoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class FoodsServiceImpl implements FoodsService {

    private static final Logger log = LoggerFactory.getLogger(FoodsServiceImpl.class);
    private static final Random random = new Random();

    @Autowired
    private FoodsMapper foodsMapper;

    @Override
    public boolean doAddFoodsInfo(Foods foods) {
        return foodsMapper.insert(foods) > 0;
    }

    @Override
    public boolean doModifyFoodsInfo(Foods foods) {
        return foodsMapper.update(foods) > 0;
    }

    @Override
    public boolean doDeleteFoodsInfo(Foods foods) {
        return foodsMapper.delete(foods) > 0;
    }

    @Override
    public Foods doGetFoodsInfo(Foods foods) {
        return foodsMapper.select(foods);
    }

    @Override
    public List<Foods> doGetFoodsInfoList() {
        // 模拟50%的失败率，用于测试熔断
        if (random.nextBoolean()) {
            log.error("模拟服务异常，触发熔断");
            throw new RuntimeException("模拟服务异常，触发熔断");
        }
        return foodsMapper.selectAll();
    }

    @Override
    public List<Foods> doQueryFoodsInfoList(Foods foods) {
        return foodsMapper.query(foods);
    }
}